#!/usr/bin/perl -w

use strict;
use warnings;
use utf8;
use open ':encoding(utf8)';
use lib '../';
use ImportHelpers;


my %bookLookup = (
	'Matt'   => "MT.txt"   ,
	'Mark'   => "MR.txt"   ,
	'Luke'   => "LU.txt"   ,
	'John'   => "JOH.txt"  ,
	'Acts'   => "AC.txt"   ,
	'Rom'    => "RO.txt"   ,
	'1Cor'   => "1CO.txt"  ,
	'2Cor'   => "2CO.txt"  ,
	'Gal'    => "GA.txt"   ,
	'Eph'    => "EPH.txt"  ,
	'Phil'   => "PHP.txt"  ,
	'Col'    => "COL.txt"  ,
	'1Thess' => "1TH.txt"  ,
	'2Thess' => "2TH.txt"  ,
	'1Tim'   => "1TI.txt"  ,
	'2Tim'   => "2TI.txt"  ,
	'Titus'  => "TIT.txt"  ,
	'Phlm'   => "PHM.txt"  ,
	'Heb'    => "HEB.txt"  ,
	'Jas'    => "JAS.txt"  ,
	'1Pet'   => "1PE.txt"  ,
	'2Pet'   => "2PE.txt"  ,
	'1John'  => "1JO.txt"  ,
	'2John'  => "2JO.txt"  ,
	'3John'  => "3JO.txt"  ,
	'Jude'   => "JUDE.txt" ,
	'Rev'    => "RE.txt"   ,   
);

my $sourceDir = ImportHelpers::getSourceDirName(__FILE__);

ImportHelpers::insertWork(
	id        => 4,
	language  => 'grc',
	osis_id   => 'Tischendorf',
	name      => "Tischendorf 8th ed. v2.5 (Qere)",
	type      => 'manuscript-edition',
	copyright => "By <a href='http://en.wikipedia.org/wiki/Constantin_von_Tischendorf' title='Constantin von Tischendorf @ Wikipedia'>Constantin von Tischendorf</a>. Based on G. Clint Yale's Tischendorf text and on Dr. Maurice A. Robinson's Public Domain Westcott-Hort text. Edited by <a href='http://www.hum.aau.dk/~ulrikp/'>Ulrik Sandborg-Petersen</a>.",
	url       => ImportHelpers::getSourceExternalURL($sourceDir),
	licenses  => [qw( http://creativecommons.org/licenses/publicdomain/ )]
);

#chdir "$sourceDir/Unicode";
foreach my $bookCode (@ImportHelpers::osisNewTestamentCodes){
	#next if $bookCode ne '1John'; #temp	
	next if !ImportHelpers::isBookOK($bookCode);
	my $filename = "$sourceDir/Unicode/$bookLookup{$bookCode}";
	die "Couldn't find $filename" if not -e $filename;
	
	open IN, $filename;
	while(<IN>){
		s/\r?\n$//; #xp-chomp
		
		# - One word per line
		# - Space-separated fields (except for the last two)
		# - - fields:
		#   0. Book (corresponds to the filename, which is the Online Bible standard)
		#   1. Chapter:Verse.word-within-verse
		#   2. Pagraph break ("P") / Chapter break ("C") / No break (".") (see
		#      below)
		#   3. The text as it is written in the printed Tischendorf (Kethiv)
		#   4. The text as the editor thinks it should have been (Qere)
		#   5. The morphological tag (following the Qere)
		#   6. The Strong's number (following the Qere)
		#   7. The lemma in two versions:
		#     7.a The first version, which corresponds to The NEW Strong's
		#       Complete Dictionary of Bible Words.
		#     7.b Followed by the string " ! "
		#     7.c Then the second version, which corresponds to Friberg, Friberg
		#       and Miller's ANLEX.
		#     There may be several words in each lemma.
		# 
		# All Strong's numbers are single numbers with 1,2,3, or 4 digits.
		# 1JO 1:1.1 . Ὃ Ὃ R-ASN 3739 ὅς ! ὅς
		#s/^1JO (\d+):(\d+)\.(\d+) ([\.CP])// or die "Parse error in Tisch";
		m{^
			(\S+)\s+                # Book (corresponds to the filename, which is the Online Bible standard)
			(\d+):(\d+)\.(\d+)\s+   # Chapter:Verse.word-within-verse
			(\S)\s+                 # Pagraph break ("P") / Chapter break ("C") / No break (".") 
			(\S+)\s+                # The text as it is written in the printed Tischendorf (Kethiv)
			(\S+)\s+                # The text as the editor thinks it should have been (Qere)
			(\S+)\s+                # The morphological tag (following the Qere)
			(\d+)\s+                # The Strong's number (following the Qere)
			(.+?)                   # Lemma which corresponds to The NEW Strong's Complete Dictionary of Bible Words. (There may be several words in each lemma.)
			\s+!\s+
			(.+)                   # Lemma which corresponds to Friberg, Friberg and Miller's ANLEX. (There may be several words in each lemma.)
		}x || die "Parse error in: $_";
		
		my $chapter = $2;
		my $verse = $3;
		my $break = $5; #C = §, P = ¶
		my $kethiv = $6;
		my $qere = $7;
		my $parsing = $8;
		my $strongs = $9;
		my $lemma1 = $10;
		my $lemma2 = $11;
		my $lemma = ($lemma1 eq $lemma2 ? $lemma1 : "$lemma1, $lemma2");
		
		#We need to store the punctuation and other things
		
		$qere =~ s/[;,\.·]//g;
		$kethiv =~ s/[;,\.·]//g;
		$parsing .= " (kethiv: $kethiv)" if $kethiv ne $qere;
		
		ImportHelpers::insertToken(
			data => $qere,
			type => 'word',
			
			book => $bookCode,
			chapter => $chapter,
			verse => $verse,
			
			parsing => $parsing,
			lemma => $lemma,
			strongs => "G$strongs"
		);

	}
	ImportHelpers::finishBookRefs();
	close IN;
	
}

1;